#pragma once
#include<iostream>
#include<assert.h>
#include<functional>
#include<algorithm>
#include<vector>
using namespace std;

namespace why
{

	class Date
	{
		friend ostream& operator<<(ostream& out, const Date& d);
	public:
		Date(int year = 0, int month = 0, int day = 0)
			: _year(year)
			, _month(month)
			, _day(day)
		{}
	private:
		int _year;
		int _month;
		int _day;
	};
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;


		vector()
		{}
		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		vector(int n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		/*vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto ch : v)
			{
				push_back(ch);
			}
		}*/
		/*vector(const vector<T>& v)
		{
			reserve(v.capacity());
			memcpy(_start, v._start, sizeof(T) * v.size());
			_finish = _start + v.size();
		}*/
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (size_t i = 0; i < v.size(); i++)
			{
				_start[i] = v._start[i];
			}
			_finish = _start + v.size();
		}
		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * size());
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = tmp + sz;
				_end_of_storage = tmp + n;
			}
		}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin()const
		{
			return _start;
		}
		const_iterator end()const
		{
			return _finish;
		}
		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}
		T& operator[](size_t pos)const
		{
			assert(pos < size());
			return _start[pos];
		}
		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			++_finish;
		}
		size_t capacity()const
		{
			return _end_of_storage - _start;
		}
		size_t size()const
		{
			return _finish - _start;
		}
		void pop_back()
		{
			assert(!empty());
			_finish--;
		}
		bool empty()
		{
			return _finish - _start;
		}
		void resize(const size_t n, T val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				if (n > capacity())
				{
					reserve(n);
				}
				while (_finish != _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
		}
		iterator insert(iterator pos, const T& val)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}
			iterator end = _finish + 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = val;
			_finish++;
			return pos;
		}
		iterator erase(iterator pos)
		{
			assert(pos <= _finish);
			assert(pos >= _start);

			iterator start = pos + 1;
			while (start != _finish)
			{
				*(start - 1) = *start;
				++start;
			}
			--_finish;
			return pos;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};
	ostream& operator<<(ostream& out, const Date& d)
	{
		out << d._year << d._month << d._day << endl;
		return out;
	}
	void Func(const vector<int>& v)
	{
		for (size_t i = 0; i < v.size(); i++)
		{
			cout << v[i] << ' ';
		}
		cout << endl;
		vector<int>::const_iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << ' ';
			it++;
		}
		cout << endl;
	}
	void test1()
	{
		vector<int> vv;
		vv.push_back(1);
		vv.push_back(1);
		vv.push_back(1);
		vv.push_back(1);
		vv.push_back(1);
		Func(vv);
		for (size_t i = 0; i < vv.size(); i++)
		{
			cout << vv[i] << ' ';
		}
		cout << endl;
		vector<int>::iterator it = vv.begin();
		while (it != vv.end())
		{
			cout << *it << ' ';
			it++;
		}
		cout << endl;
	}
	void test2()
	{
		/*std::vector<int> v = {1,1,1,1};
		v.resize(10, 2);
		std::vector<int>::iterator it = v.begin();
		for (auto ch : v)
		{
			cout << ch << ' ';
		}
		cout << endl;*/
		vector<Date> v;
		/*v.push_back({ 2023, 2, 14 });
		v.push_back({ 2023, 2, 14 });
		v.push_back({ 2023, 2, 14 });*/
		v.resize(2, { 2023, 2, 15 });
		why::vector<Date>::const_iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it;
			it++;
		}
		cout << endl;
	}
	void test3()
	{
		std::vector<int> v;
		v.push_back(0);
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		//v.push_back(5);
		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		std::vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{
				it = v.erase(it);
			}
			else
			{
				it++;
			}
		}
		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
	}
	void test4()
	{
		vector<int> v(10, 10);
		int a[] = { 1,30,14,23,5,24 };
		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		vector<int> v1(v.begin(), v.end());
		for (auto ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;
		vector<int> v2(v.begin() + 1, v.end() - 1);
		for (auto ch : v2)
		{
			cout << ch << " ";
		}
		cout << endl;
		vector<int> v4(a, a + 5);
		for (auto ch : v4)
		{
			cout << ch << " ";
		}
		cout << endl;
		sort(v4.begin(), v4.end());
		for (auto ch : v4)
		{
			cout << ch << " ";
		}
		cout << endl;
		greater<int> g;
		sort(v4.begin(), v4.end(), g);
		for (auto ch : v4)
		{
			cout << ch << " ";
		}
		cout << endl;

	}
	void test5()
	{
		vector<int> v1(10, 10);
		for (auto ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;
		vector<int> v2(v1);
		for (auto ch : v2)
		{
			cout << ch << " ";
		}
		cout << endl;
	}
	void test6()
	{
		vector<std::string> v(3, "hello world");
		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		vector<std::string> v1(v);
		for (auto ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;
		v1.push_back("2222222222222222222");
		for (auto ch : v1)
		{
			cout << ch << " ";
		}
		cout << endl;
	}
}

